/**
 * Created by liaoxd on 2017/12/17.
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/datafilter/dataFilter.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    // 创建virtual filter model
    var vm = yufp.custom.vue({
      // TODO 请替换此id属性
      el: '#dataauthtmpl_grid',
      // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
      data: function () {
        var me = this;
        return {
          // 数据模板URL
          serviceUrl: backend.appOcaService + '/api/adminsmdataauthtmpl/',
          // 控制点URL
          contrUrl: backend.appOcaService + '/api/adminsmrescontr/',
          idView: false,
          height: yufp.custom.viewSize().height - 140,

          userId: yufp.session.userId,
          formdata: {},
          tableColumns: [
            { label: this.$t('dataFilter.mbm'), prop: 'authTmplName', sortable: true, resizable: true },
            { label: this.$t('dataFilter.zwfmc'), prop: 'sqlName'},
            { label: this.$t('dataFilter.yxj'), prop: 'priority', width: 80 },
            { label: this.$t('dataFilter.sjqxtj'), prop: 'sqlString', minWidth: 200},
            { label: this.$t('dataFilter.zxbgyh'), prop: 'lastChgUsr'},
            { label: this.$t('dataFilter.zxbgsj'), prop: 'lastChgDt'}

          ],
          contrlTableColumns: [
            { label: this.$t('dataFilter.kzd'), prop: 'contrId', width: 200, sortable: true, resizable: true },
            { label: this.$t('dataFilter.kzddm'), prop: 'contrCode', width: 200 },
            { label: this.$t('dataFilter.kzdmc'), prop: 'contrName', width: 250 }

          ],
          updateFields: [{
            columnCount: 1,
            fields: [
              {
                field: 'authTmplName',
                label: this.$t('dataFilter.mbm'),
                rules: [{ required: true, message: this.$t('dataFilter.btx'), trigger: 'blur' },
                  { max: 50, message: this.$t('dataFilter.zdcdbcggzwzf'), trigger: 'blur' }]
              },
              {
                field: 'sqlName',
                label: this.$t('dataFilter.zwfmc'),
                rules: [{ required: true, message: this.$t('dataFilter.btx'), trigger: 'blur' },
                  { max: 50, message: this.$t('dataFilter.zdcdbcggzwzf'), trigger: 'blur' }]
              },

              {
                field: 'priority',
                label: this.$t('dataFilter.yxj'),
                rules: [{ message: this.$t('dataFilter.qsrsz'), validator: yufp.validator.number }]
              },
              {
                field: 'sqlString',
                label: this.$t('dataFilter.sjtj'),
                rules: [{ required: true, message: this.$t('dataFilter.btx'), trigger: 'blur' }]
              }
            ]
          }],
          updateButtons: [
            {
              label: this.$t('dataFilter.bc'),
              type: 'primary',
              op: 'submit',
              hidden: false,
              click: function (model, valid) {
                if (valid) {
                  model.contrInclude = '*';
                  model.lastChgUsr = me.userId;
                  me.createFilter(model);
                }
              }
            },
            {
              label: this.$t('dataFilter.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model, valid) {
                if (valid) {
                  model.lastChgUsr = me.userId;
                  me.dataFliterEditFn(model);
                  me.dialogVisible = false;
                  me.$refs.filterTable.remoteData();
                }
              }
            },
            {
              label: this.$t('dataFilter.qx'),
              hidden: false,
              click: function (model) {
                me.dialogVisible = false;
              }
            }
          ],
          selectionButton: [
            {
              label: this.$t('dataFilter.qx'),
              hidden: false,
              click: function (model) {
                me.tmplDialogVisible = false;
              }
            },
            {
              label: this.$t('dataFilter.qrxz'),
              type: 'primary',
              hidden: false,
              click: function (model) {
                me.$refs.datafilterForm.formModel.contrInclude = me.filterGrid.contrlSelection;
                me.tmplDialogVisible = false;
              }
            }

          ],
          // 表单是否显示
          dialogVisible: false,
          // 表单是否可用
          formDisabled: false,
          // 表单操作状态
          viewType: 'DETAIL',
          // 可用控制点
          tmplDialogVisible: false,

          filterGrid: {
            // 权限模板当前行
            currentRow: null,
            // 权限模板选择数组
            selections: [],
            // 权限模板多选ID
            multipleSelection: '',
            // 控制点多选ID
            contrlSelection: '',
            data: null,

            total: null,
            loading: true,
            subloading: true,

            paging: {
              page: 1,
              pageSize: 10
            },
            // 系统参数模糊查询表头
            query: {
              authTmplName: '',
              sqlName: ''
            }

          }
        };
      },
      mounted: function () {
      },
      methods: {
        startChangeFn: function (val) {
          this.filterGrid.paging.page = val;
          this.queryfilterGridFn();
        },
        limitChangeFn: function (val) {
          this.filterGrid.paging.page = 1;
          this.filterGrid.paging.pageSize = val;
          this.queryfilterGridFn();
        },
        rowClickFn: function (selection, row) {
          // 用于单个修改
          this.filterGrid.currentRow = row;
        },

        // 控制功能点多选择
        contrlSelete: function (selection, row) {
          var selections = '';
          if (selection.length > 0) {
            for (var i = 0; i < selection.length; i++) {
              // 记录多选用于控制功能点选择
              if (i == 0) {
                selections = selection[i].contrId;
              } else {
                selections = selections + ',' + selection[i].contrId;
              }
            }
          }
          this.filterGrid.contrlSelection = selections;
        },

        // 控制功能点展示
        tmplShow: function () {
          this.$nextTick(function () {
            this.$refs.contrlTable.remoteData();
          });
        },

        // 修改按钮
        lookuptableEditFn: function () {
          if (this.$refs.filterTable.selections.length < 1) {
            vm.$message({ message: vm.$t('dataFilter.qxzytjlxg') });
            return false;
          }
          if (this.$refs.filterTable.selections.length > 1) {
            vm.$message({ message: vm.$t('dataFilter.znxzytjlxg') });
            return false;
          }
          this.viewType = 'UPDATE';
          vm.dialogVisible = true;
          this.$nextTick(function () {
            yufp.extend(this.$refs.datafilterForm.formModel, this.$refs.filterTable.selections[0]);
            vm.$refs.datafilterForm.switch('authTmplName', 'disabled', true);
          });
        },


        // 修改保存
        dataFliterEditFn: function (row) {
          yufp.service.request({
            url: backend.appOcaService + '/api/adminsmdataauthtmpl/updates',
            method: 'post',
            data: row,
            callback: function (code, message, response) {
              if (code == '0') {
                if (response.code == '100000') {
                  vm.$message({ message: response.message, type: 'warning' });
                  return false;
                } else {
                  vm.$message({ message: vm.$t('dataFilter.xgcg') });
                  vm.$refs.filterTable.remoteData();
                }
              } else {
                vm.$message({ message: vm.$t('dataFilter.xgsb') });
              }
            }
          });
        },

        // 新增按钮
        addFilterRecord: function () {
          this.dialogVisible = true;
          this.viewType = 'ADD';

          this.$nextTick(function () {
            this.$refs.datafilterForm.resetFields();
            this.$refs.datafilterForm.switch('authTmplName', 'disabled', false);
          });
        },

        // 保存新增系统参数
        createFilter: function (row) {
          this.filterGrid.currentRow = row;

          delete this.filterGrid.currentRow.authTmplId;
          yufp.service.request({
            url: backend.appOcaService + '/api/adminsmdataauthtmpl/',
            method: 'post',
            data: this.filterGrid.currentRow,
            callback: function (code, message, response) {
              if (code == 0) {
                if (response.code == 0) {
                  vm.$message({ message: vm.$t('dataFilter.bccg') });
                  vm.$refs.filterTable.remoteData();
                  vm.dialogVisible = false;
                } else {
                  vm.$message({ message: response.message, type: 'warning' });
                  return false;
                }
              } else {
                vm.$message({ message: vm.$t('dataFilter.bcsb') });
                return false;
              }
            }
          });
        },

        // 批量删除模板
        dataFiltermultDeleteFn: function () {
          var ids = '';
          var filterSelecttions = this.$refs.filterTable.selections;
          if (filterSelecttions.length > 0) {
            for (var i = 0; i < filterSelecttions.length; i++) {
              // 记录多选用于多删
              if (filterSelecttions.length === 1) {
                ids = filterSelecttions[i].authTmplId;
              } else {
                ids = ids + ',' + filterSelecttions[i].authTmplId;
              }
            }
          } else {
            vm.$message({ message: vm.$t('dataFilter.qxzxyscdmb') });
            return false;
          }
          vm.$confirm(vm.$t('dataFilter.scmbjhscqsjqxyyyjsqgxqrplscsjqxmbqjscz'), vm.$t('dataFilter.ts'), {
            confirmButtonText: vm.$t('dataFilter.qd'),
            cancelButtonText: vm.$t('dataFilter.qx'),
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              url: backend.appOcaService + '/api/adminsmdataauthtmpl/deletes/' + ids,
              method: 'post',
              data: {
                id: ids
              },
              callback: function (code, message, response) {
                if (code == '0') {
                  vm.$message({ message: vm.$t('dataFilter.sccg') });
                  vm.$refs.filterTable.remoteData();
                } else {
                  vm.$message({ message: vm.$t('dataFilter.scsb') });
                }
              }
            });
          });
        }


      },
      filters: {
        dateFilter: function (d) {
          return d;
        }
      },
      watch: {
        viewType: function (value) {
          if (value == 'ADD') {
            this.updateButtons[0].hidden = false;
            this.updateButtons[1].hidden = true;
          } else if (value == 'UPDATE') {
            this.updateButtons[0].hidden = true;
            this.updateButtons[1].hidden = false;
          } else if (value == 'DETAIL') {
            this.updateButtons[0].hidden = true;
            this.updateButtons[1].hidden = true;
          }
        }

      }
    });
  };


  // 消息处理
  exports.onmessage = function (type, message) {

  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
